/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundaries;

import business.UserManager;
import entity.Event;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import javax.inject.Named;

/**
 * This bean handles the oprations of the user
 * @author Alessandro
 */
@ManagedBean
@SessionScoped
public class UserBean implements Serializable{
    
    @EJB
    UserManager um;
    
    private List<Event> calendar;
    
    private String searchText;
    
    public UserBean(){
        
    }
    
    public String getName(){
        return um.getLoggedUser().getFirstName()+" "+ um.getLoggedUser().getLastName();
    }
    
    public String getVisibility(){
        System.out.println("ciao sono userBean, col get visibility");
        return um.getLoggedUser().getVisibility().toString();
    }
    
    /**
     * this method change the visisbilty of the user's calendar
     * @return 
     */
    public String changeVisibility(){
        um.changeVisibility();
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Privacy changed!"));
        return "profile";
    }

    public String getSearchText() {
        return searchText;
    }

    public void setSearchText(String searchText) {
        this.searchText = searchText;
    }

    public List<Event> getCalendar() {
        return calendar;
    }

    public void setCalendar(List<Event> myEvents) {
        this.calendar = myEvents;
    }
    
    
    /**
     * only redirect to myEvents page
     * @return 
     */
    public String loadCalendar(){
       return "myEvents?faces-redirect=true";
    }
    
    public String search() {
        String text = searchText;
        searchText="";
        return "searching?faces-redirect=true&text="+text;
    }
    
    /**
     * The function that gives the home page the string representing the image
     * showing how many not read messages the user has.
     * @return the source of the image to show within the messages-Button
     */
    public String checkPendingMessages() {
        int pendingsNumber = um.checkPendingMessages();
        System.out.println("not read number_:" + pendingsNumber);
        if (pendingsNumber > 3) {
            return "over3";
        } else {
            return ""+pendingsNumber;
        }
    }

}
